﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace CSMF.Management.Page_Team
{
    public partial class Team_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                lblILogo.Text = FileUploads(this, fuInsertLogo, "..\\..\\UpLoad\\teamImage\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = "../../UpLoad/teamImage/" + lblILogo.Text;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.PostedFile.FileName != "")
            {
                Label1.Text = FileUploads(this, FileUpload1, "..\\..\\UpLoad\\teamImage\\");
                Label2.Text = "图片上传成功！";
                this.Image1.ImageUrl = "../../UpLoad/teamImage/" + Label1.Text;
            }
            else
            {
                Label2.Text = "请选择要上传的图片！";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text.Trim() != "")
            {
                Team team = new Team();
                team.Title = txtTitle.Text.Trim();
                team.Purpose = txtPurpose.Text.Trim();
                team.TeamContent = FCKeditor1.Value;
                team.Image = lblILogo.Text;
                team.TeamPhoto = Label1.Text;
                TeamManager.AddTeam(team);
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('添加团队信息成功。');window.opener.location.replace('Team_List.aspx');window.close();", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请输入团队的标题。');", true);
            }

        }


        #region FileUp
        public static string FileUploads(Page page, FileUpload fileProduct, string url)
        {
            if (!fileProduct.HasFile)
            {
                return "出错";

            }
            try
            {
                string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
                fileProduct.SaveAs(page.Server.MapPath(url) + urlimg);
                return urlimg;
            }
            catch
            {
                return "出错";
            }
        }
        #endregion
    }
}
